<?php
/**
 * @author GhostWei
 * @date Oct 27, 2012
 */
class oauth_request_iweibo extends oauth_api_request {
	
	public function init() {
		$this->_name = 'iweibo';
		$this->_params['oauth_consumer_key'] = $this->getConfig()->getAppId();
		$this->_params['access_token'] = $this->getOauth()->getAccessToken();
		$this->_params['openid'] = $this->getOauth()->getAppId();
		$this->_params['clientip'] = system_controller_front::getInstance()->getRequest()->getClientIp(true);
		$this->_params['oauth_version'] = '2.a';
		$this->_params['scope'] = 'all';
		$this->_params['format'] = 'json';
		$this->_url[parent::PUBLIC_TWEET_LIST] = 'https://open.t.qq.com/api/statuses/public_timeline';
		$this->_url[parent::USER_LINE] = 'https://open.t.qq.com/api/user/other_info';
		$this->_url[parent::USER_TWEET_LIST] = 'https://open.t.qq.com/api/statuses/user_timeline';
		$this->_url[parent::TWEET_LINE] = 'https://open.t.qq.com/api/t/show';
		$this->_url[parent::HOT_TOPIC_LIST] = 'https://open.t.qq.com/api/trends/ht';
		$this->_url[parent::TOPIC_TWEET_LIST] = 'http://open.t.qq.com/api/statuses/ht_timeline';
		
		$this->_flips = array('limit' => 'reqnum', 'uid' => 'name', 'keyword' => 'httext');
	}

	/**
	 * @return oauth_api_response
	 */
	public function fetch($action, array $params) {
		if($action === parent::USER_TWEET_LIST) {
			$this->flip_params($params);
			$req = (array) system_controller_front::getInstance()->getRequest()->getQuery();
			if(!isset($req['pageflag'])) $req['pageflag'] = 0;
			if(!isset($req['pagetime'])) $req['pagetime'] = 0;
			if(!isset($req['lastid'])) $req['lastid'] = 0;
			
			$this->_params['type'] = 0;
			$this->_params['contenttype'] = 0;
			$this->_params = array_merge($this->_params, $req);
			$class = 'oauth_open_' . $this->_name;
			$api = new $class();
			return $api->request((isset($this->_url[$action]) ? $this->_url[$action] : null), $this->_params);
		}
		if($action === parent::TOPIC_TWEET_LIST) {
			$this->flip_params($params);
			$req = (array) system_controller_front::getInstance()->getRequest()->getQuery();
			if(!isset($req['pageinfo'])) $req['pageinfo'] = null;
			if(!isset($req['pageflag'])) $req['pageflag'] = 4;
			
			$this->_params = array_merge($this->_params, $req);
			$class = 'oauth_open_' . $this->_name;
			$api = new $class();
			return $api->request((isset($this->_url[$action]) ? $this->_url[$action] : null), $this->_params);
			
		}
		return parent::fetch($action, $params);
	}
	
	
	
}
